package cn.lyjuan.android.demofive;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

public class TagActivity extends Activity
{
    ListView tagList;

    public static final String EXTRA_TITLE = "title";
    public static final String EXTRA_TAG = "tag";

    public static final String EXTRA_ITEMS = "items";

    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        ArrayList<Res.IItem> items = null;
        String title = null;

        Intent intent = this.getIntent();
        if (null != intent)
        {
            items = (ArrayList<Res.IItem>) intent.getSerializableExtra(EXTRA_ITEMS);
            title = (String) intent.getSerializableExtra(EXTRA_TITLE);
        }

        if (null == items)// 没有数据，显示首页
        {
            Res.TagInfo item = Res.getIndex();
            items = item.items;
            title = item.name;
        }
        this.setTitle(title);

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        tagList = (ListView) this.findViewById(R.id.list);

        String[] names = new String[items.size()];
        for (int i = 0; i < items.size(); i++)
            names[i] = items.get(i).getName();

        ArrayAdapter<String> adp = new ArrayAdapter<>(this,
                android.R.layout.simple_list_item_1, names);

        tagList.setAdapter(adp);

        tagList.setOnItemClickListener(new ListItemClickListener(items));
    }
}
